import sys
sys.path.append('..')
from stanfordcorenlp import StanfordCoreNLP
import Constant
import ParaSplit
import wordAnalyzer
path ='C:/Users/santongding/Desktop/stanford-corenlp-4.2.2'
nlp = StanfordCoreNLP(path)


def GetPartofspeechVector(s):
    #print('!!!!!!!!! '+s)

        
    postag = nlp.pos_tag(s)
    
    dic=dict()

    #print(postag)
    ans=[]
    unkowncnt=0
    for pr in postag:
        if pr[1] not in Constant.PartOfSpeechList:
            print('[!!!]'+pr[0]+' '+pr[1])
            unkowncnt+=1
            
            #raise Exception('Error')
        if pr[1] in dic:
            dic[pr[1]]=dic[pr[1]]+1
        else :
            dic[pr[1]]=1

    sum=0
    for x in Constant.PartOfSpeechList:
        if x in dic:
            sum+=dic[x]/len(postag)
            ans.append(dic[x]/len(postag))
        else :
            ans.append(0)
    ans.append(unkowncnt/len(postag))
    return ans
 
    
'''
    print(dic)
    print(sum)
    print(ans)
    
'''

def GetStructureVector(s):
    maxdpsum=0
    sumdp=0
    leafcnt=0
    for x in s:
        cnt=0
        maxdp=0
        lst=0
        for c in nlp.parse(x):
            
            if c=='(':
                cnt+=1
                lst='('
            elif c==')':
                if lst!=c:
                    leafcnt+=1
                    sumdp+=cnt
                    if cnt>maxdp:
                        maxdp=cnt
                cnt-=1
                lst=')'
       # print(maxdp)
        maxdpsum+=maxdp

    return [sumdp/leafcnt,maxdpsum/len(s)]

    

def GetParaVector(s):
    sum=0
    for x in s:
        sum+=len(x.split())
    sum/=len(s)
    return [sum]

    

def GetVector(s):#参数是列表
    ns=''
    for x in s:
        ns+=x
    
    tp=GetParaVector(s)
    tp.extend(GetStructureVector(s))
    tp.extend(GetPartofspeechVector(ns))
    tp.extend(wordAnalyzer.GetWordVector(ns))
    return tp
def GetVtr(s):#参数是string
    ns=ParaSplit.ParasSplit(s)
    tp=GetParaVector(ns)
    tp.extend(GetStructureVector(ns))
    tp.extend(GetPartofspeechVector(s))
    tp.extend(wordAnalyzer.GetWordVector(s))
    return tp

#print(GetParaVector(s))

#print(GetVector(ParaSplit.ParasSplit(open('in.txt','r',encoding='UTF-8').read())))


